#include<bits/stdc++.h>
using namespace std;

const int N = 310;
int s[N];
int f[N][N];
int n;

int main()
{
    
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &s[i]);
    // 统计前缀和
    for (int i = 1; i <= n; i ++ ) s[i] += s[i - 1];
    
    // 遍历区间长度
    for (int len = 2; len <= n; len ++ )
        // 遍历区间端点
        for (int i = 1; i + len - 1 <= n; i ++ ) 
        {
            int l = i, r = i + len - 1;
            f[l][r] = 1e8;
            // 遍历区间里的间隔点
            for (int k = l; k < r; k ++ ) 
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
        }
    
    printf("%d\n", f[1][n]);
    
    return 0;
}